package com.wtp.wallpaper.controller.admin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtp.wallpaper.pojo.dto.ScheduledDTO;
import com.wtp.wallpaper.pojo.entity.ScheduledTime;
import com.wtp.wallpaper.pojo.query.PageQuery;
import com.wtp.wallpaper.pojo.vo.ScheduledTimeVO;
import com.wtp.wallpaper.pojo.result.Result;
import com.wtp.wallpaper.service.ScheduledService;
import com.wtp.wallpaper.utils.PojoConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/admin/scheduled")
public class ScheduledController {

    @Autowired
    private ScheduledService scheduledService;
    @GetMapping("/getScheduledList")
    public Result getScheduledList(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "5") int pageSize) {
        Page<ScheduledTime> page = scheduledService.page(new PageQuery(pageNum, pageSize).toMpPageDefaultSortByCreateTime());
        Page<ScheduledTimeVO> convert = PojoConvertUtil.convert(page, ScheduledTimeVO.class);

        List<ScheduledTimeVO> collect = convert.getRecords().stream().map((scheduledTimeVO -> {
            scheduledTimeVO.setEnable(scheduledTimeVO.getState() == 1);
            return scheduledTimeVO;
        })).collect(Collectors.toList());
        convert.setRecords(collect);
        return Result.success(convert);
    }

    @PostMapping("/modifyState")
    public Result modifyState(ScheduledDTO scheduledDTO) throws ClassNotFoundException {
        log.info("传来数据:{}",scheduledDTO);
        scheduledService.updateState(scheduledDTO);
        return Result.success();
    }

    @PostMapping("/modifyOne")
    public Result modifyOne(ScheduledDTO scheduledDTO){
        log.info("传来数据:{}",scheduledDTO);
        scheduledService.update(scheduledDTO);
        return Result.success();
    }

}
